<?php

namespace Fn;

class String extends Base {

    static function format($str, $arr, $tpl = '{*}') {
        $keys = array_keys($arr);
        list($pre, $suf) = explode('*', $tpl);
        foreach ($keys as $k => $v) {
            $keys[$k] = $pre . $v . $suf;
        }

        return str_replace($keys, array_values($arr), $str);
    }

    static function rand() {
        return md5(microtime(true) . '');
    }

    static function notEmpty($str) {
        return !empty($str);
    }

    static function exp($exp, $t, $f) {
        return $exp ? $t : $f;
    }

    /**
     * 字符串截取，支持中文和其他编码
     * @static
     * @access public
     * @param string $str 需要转换的字符串
     * @param string $start 开始位置
     * @param string $length 截取长度
     * @param string $charset 编码格式
     * @param string $suffix 截断显示字符
     * @return string
     */
    static public function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) {
        if (function_exists("mb_substr"))
            $slice = mb_substr($str, $start, $length, $charset);
        elseif (function_exists('iconv_substr')) {
            $slice = iconv_substr($str, $start, $length, $charset);
        } else {
            $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
            $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
            $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
            $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
            preg_match_all($re[$charset], $str, $match);
            $slice = join("", array_slice($match[0], $start, $length));
        }
        return $suffix ? $slice . '...' : $slice;
    }

    /**
     * 检查字符串是否是UTF8编码
     * @param string $string 字符串
     * @return Boolean
     */
    static public function isUtf8($str) {
        $c = 0;
        $b = 0;
        $bits = 0;
        $len = strlen($str);
        for ($i = 0; $i < $len; $i++) {
            $c = ord($str[$i]);
            if ($c > 128) {
                if (($c >= 254))
                    return false;
                elseif ($c >= 252)
                    $bits = 6;
                elseif ($c >= 248)
                    $bits = 5;
                elseif ($c >= 240)
                    $bits = 4;
                elseif ($c >= 224)
                    $bits = 3;
                elseif ($c >= 192)
                    $bits = 2;
                else
                    return false;
                if (($i + $bits) > $len)
                    return false;
                while ($bits > 1) {
                    $i++;
                    $b = ord($str[$i]);
                    if ($b < 128 || $b > 191)
                        return false;
                    $bits--;
                }
            }
        }
        return true;
    }

    /**
     * separatorConverter
     * 分隔符转换器
     * @param unknown_type $str
     * @param unknown_type $from
     * @param unknown_type $to 'U用大小写分隔   如果要用U做分隔请\U'
     */
    static public function separatorConverter($str, $from = '_', $to = 'U') {
        if ($from === $to)
            return $str;
        if ($from === 'U')
            return preg_replace('/[A-Z]/', $to . '$1', $subject);
        if ($to === 'U')
            return $str[0] . substr(
                            str_replace('_', '', ucwords($str)), 1
            );

        $from = $from === '\U' ? 'U' : $from;
        $to = $to === '\U' ? 'U' : $to;
        return str_replace($from, $to, $str);
    }

    static public function passwordHash($p) {
        $str = $p . 'AND--密码哈希值--passwordHash--密码哈希值--passwordHash--密码哈希值';
        return md5(md5($p) . md5($str) . md5($p[0]));
    }
    
    
    /*
     * 给字符串前面 补零
     * $str: 字符串
     * $len: 补零后的总长度
     * */
    static public function zerofill($str,$len){
        if(strlen($str) >= $len){
            return $str;
        }
        $l = $len-strlen($str);
        for ($i=0; $i<$l; $i++){
            $str = '0'.$str;
        }
    
        return $str;
    }

    /**
     * [htmlToTxt html转文本]
     * @param  [string] $str [html源码]
     * @return [string]      [文本格式]
     */
    static public function htmlToTxt($str){
        $tmp = preg_replace("/<(a|span|b|big|em|i|label|font)\s+.*?>/","", $str);
        $tmp = preg_replace("/<\s*\/\s*(a|span|b|big|em|i|label|font)>/","", $tmp);
        $tmp = preg_replace("/<(S*?)[^>]*>.*?|<.*?>i/","\n", $tmp);          
        $tmp = preg_replace("/&nbsp;/"," ", $tmp);
        $tmp = trim($tmp);
        $tmp = preg_replace("/\s*\n\s*/","<br/>", $tmp);

        return $tmp;
    }
     
 

}